{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                  price   R-squared:                       0.442\n",
      "Model:                            OLS   Adj. R-squared:                  0.407\n",
      "Method:                 Least Squares   F-statistic:                     12.68\n",
      "Date:                Mon, 17 Jan 2022   Prob (F-statistic):           1.17e-07\n",
      "Time:                        00:53:07   Log-Likelihood:                -627.66\n",
      "No. Observations:                  69   AIC:                             1265.\n",
      "Df Residuals:                      64   BIC:                             1276.\n",
      "Df Model:                           4                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const       1.193e+04   5774.178      2.067      0.043     399.260    2.35e+04\n",
      "x1             4.9595      1.120      4.430      0.000       2.723       7.196\n",
      "x2          -115.0177     38.565     -2.982      0.004    -192.059     -37.976\n",
      "x3          -106.7122     81.158     -1.315      0.193    -268.845      55.420\n",
      "x4           910.9859    304.527      2.991      0.004     302.623    1519.349\n",
      "==============================================================================\n",
      "Omnibus:                        6.695   Durbin-Watson:                   1.121\n",
      "Prob(Omnibus):                  0.035   Jarque-Bera (JB):                5.889\n",
      "Skew:                           0.653   Prob(JB):                       0.0526\n",
      "Kurtosis:                       3.584   Cond. No.                     6.71e+04\n",
      "==============================================================================\n",
      "\n",
      "Notes:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "[2] The condition number is large, 6.71e+04. This might indicate that there are\n",
      "strong multicollinearity or other numerical problems.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "import statsmodels.api as sm\n",
    "\n",
    "auto = pd.read_stata(\"dataset/auto.dta\")\n",
    "\n",
    "# 自变量\n",
    "x = np.column_stack((auto['weight'], auto['length'], auto['mpg'], auto['rep78']))\n",
    "y = auto['price'] # 因变量\n",
    "c = sm.add_constant(x) # 添加常数项\n",
    "est1 = sm.OLS(y, c, missing=\"drop\") # 对于缺失值处理方式是删除\n",
    "est2 = est1.fit()\n",
    "print(est2.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6   7\n",
      "   8   9]\n",
      "[-20.29543302 -18.96305456 -15.20509478 -13.13112459 -11.29051544\n",
      " -10.70190793  -8.48007846  -4.82093167  -3.66872109  -2.47287578\n",
      "  -1.58870516   2.08870555   5.01796413   5.17419442   7.83653871\n",
      "  11.46950035  11.46577247  13.64567579  17.23534413  18.52399302]\n",
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                      y   R-squared:                       0.995\n",
      "Model:                            OLS   Adj. R-squared:                  0.995\n",
      "Method:                 Least Squares   F-statistic:                     3593.\n",
      "Date:                Sat, 22 Jan 2022   Prob (F-statistic):           3.53e-22\n",
      "Time:                        04:48:05   Log-Likelihood:                -24.510\n",
      "No. Observations:                  20   AIC:                             53.02\n",
      "Df Residuals:                      18   BIC:                             55.01\n",
      "Df Model:                           1                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const          0.1016      0.195      0.521      0.609      -0.308       0.511\n",
      "x1             2.0192      0.034     59.941      0.000       1.948       2.090\n",
      "==============================================================================\n",
      "Omnibus:                        1.700   Durbin-Watson:                   2.077\n",
      "Prob(Omnibus):                  0.427   Jarque-Bera (JB):                0.968\n",
      "Skew:                          -0.083   Prob(JB):                        0.616\n",
      "Kurtosis:                       1.935   Cond. No.                         5.81\n",
      "==============================================================================\n",
      "\n",
      "Notes:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#OLS ordinary least square model\n",
    "import numpy as np\n",
    "import statsmodels.api as sm\n",
    "#.api不能省\n",
    "x = np.arange(-10, 10)\n",
    "print(x)\n",
    "#假设y与x之间关系如下\n",
    "y = 2*x + np.random.normal(size=len(x))\n",
    "print(y)\n",
    "#python包中的ols模型默认没有常数项，如果要加，需要下面这一样\n",
    "X = sm.add_constant(x)\n",
    "#通过OLS回归模型获得各个参数\n",
    "model = sm.OLS(y, X)\n",
    "fit = model.fit()\n",
    "print (fit.summary())\n",
    "#回归的结果用表格输出，包括R平方，一些检测的P值等等\n",
    "# fittedValue = fit.fittedValues\n",
    "#拟合值，y umlaut\n",
    "# residual = fit.resid\n",
    "#残差值\n",
    "# parameter = fit.params\n",
    "#各个参数\n",
    "#使用dir(fit)可以看到还有哪些method或者是attributes是available的\n",
    " \n",
    "#如果有dummy variable，使用下面的函数生成dummy variable的matix\n",
    "#如果有n个observations（dummyOriginal是n行的），dummy variable有m种类型，那么下面生成的dummy会是n*m的矩阵\n",
    "#drop=True表示原来的dummy original不会出现在新生成的矩阵的第一列（0列）\n",
    "# dummy = sm.categorical(dummyOriginal, drop = True)\n",
    "#dummy跟其他变量放在一起的时候用np.hstack连接。\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
  },
  "kernelspec": {
   "display_name": "Python 3.7.3 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
